package com.hdjd.controller;

import com.hdjd.entities.*;
import com.hdjd.service.AccountService;
import com.hdjd.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("accountInfo")
@Controller
public class AccountController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private OrderService orderService;

    @ResponseBody
    @RequestMapping(value = "registerDeliveryman",method = RequestMethod.PUT)
    public Map<String,Object> registerDeliveryman(Integer accId){
        Map<String,Object> map = new HashMap<>();
        boolean flag;
        try{
            flag = this.accountService.registerDeliverymanByAccId(accId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "注册配送员失败");
            return map;
        }
        if (flag){
            map.put("code", 1001);
            map.put("msg", "注册配送员成功");
            return map;
        }else {
            map.put("code", 1002);
            map.put("msg", "注册配送员失败");
            return map;
        }
    }

    @ResponseBody
    @RequestMapping(value = "withdrawByAccIdAndAccPwd",method = RequestMethod.PUT)
    public Map<String,Object> withdrawByAccIdAndAccPwd(Integer accId, String accPwd, double money){
        Map<String,Object> map = new HashMap<>();
        boolean flag;
        try{
            flag = this.accountService.withdrawByAccIdAndAccPwd(accId, accPwd, money);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "提现失败，余额不足");
            return map;
        }
        if (flag){
            map.put("code", 1001);
            map.put("msg", "提现成功");
            return map;
        }else {
            map.put("code", 1002);
            map.put("msg", "提现失败，新增资金记录失败");
            return map;
        }
    }

    @ResponseBody
    @RequestMapping(value = "toUPByAccIdAndAccPwd",method = RequestMethod.PUT)
    public Map<String,Object> toUPByAccIdAndAccPwd(Integer accId, double money){
        Map<String,Object> map = new HashMap<>();
        boolean flag;
        try{
            flag = this.accountService.toUPByAccIdAndAccPwd(accId,money);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "充值失败，充值金额有误");
            return map;
        }
        if (flag){
            map.put("code", 1001);
            map.put("msg", "充值成功");
            return map;
        }else {
            map.put("code", 1002);
            map.put("msg", "充值失败，新增资金记录失败");
            return map;
        }
    }

    @ResponseBody
    @RequestMapping(value = "selectAccBalanceByAccId",method = RequestMethod.GET)
    public Map<String,Object> selectAccBalanceByAccId(Integer accId){
        Map<String,Object> map = new HashMap<>();
        String accBalance;
        try{
            accBalance = this.accountService.selectAccBalanceByAccId(accId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "余额查询失败");
            return map;
        }
        if (accBalance != null){
            map.put("code", 1001);
            map.put("msg", "余额查询成功");
            map.put("accBalance",accBalance);
            return map;
        }else {
            map.put("code", 1002);
            map.put("msg", "余额失败");
            return map;
        }
    }

    @ResponseBody
    @RequestMapping(value = "selectFundRecordByAccId",method = RequestMethod.GET)
    public Map<String,Object> selectFundRecordByAccId(Integer accId, String startDate, String endDate, @RequestParam("page") Integer pageCurrent, @RequestParam("limit") Integer pageSize){
        Map<String,Object> map = new HashMap<>();
        List<FundRecord> fundRecordList;
        Integer countNum;
        try{
            fundRecordList = this.accountService.selectFundRecordByAccId(accId, startDate, endDate, pageCurrent, pageSize);
            countNum = this.accountService.selectCountFundRecordByAccId(accId, startDate, endDate);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "资金流水查询失败");
            return map;
        }
        if (fundRecordList != null){
            map.put("code", 0);
            map.put("msg", "资金流水查询成功");
            map.put("data",fundRecordList);
            map.put("count",countNum);
            return map;
        }else {
            map.put("code", 1002);
            map.put("msg", "资金流水失败");
            return map;
        }
    }

    @ResponseBody
    @RequestMapping(value = "selectAccountByAccId",method = RequestMethod.GET)
    public Map<String,Object> selectAccountByAccId(Integer accId){
        Map<String,Object> map = new HashMap<>();
        Account account;
        try{
            account = this.accountService.selectAccountByAccId(accId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "账户信息查询失败");
            return map;
        }
        if (account != null){
            map.put("code", 1001);
            map.put("msg", "账户信息查询成功");
            map.put("account",account);
            return map;
        }else {
            map.put("code", 1002);
            map.put("msg", "账户信息查询失败");
            return map;
        }
    }

    @ResponseBody
    @RequestMapping(value = "editAccountByAccount",method = RequestMethod.POST)
    public Map<String,Object> editAccountByAccount(Integer accId,@RequestParam(value = "accImg",required = false) MultipartFile accImg, String accTruename, Integer accSex,
                                                   String accTelephone, String accEmail, String accBirthday, HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        int updateAfter;
        try{
            updateAfter = this.accountService.editAccountByAccount(accId, accImg, accTruename, accSex, accTelephone, accEmail, accBirthday, request);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "账户信息更新失败");
            return map;
        }
        if (updateAfter != 0){
            map.put("code", 1001);
            map.put("msg", "账户信息更新成功");
            return map;
        }else {
            map.put("code", 1002);
            map.put("msg", "账户信息更新失败");
            return map;
        }
    }

    @ResponseBody
    @RequestMapping(value = "/selectDefaultReceiveAddressByAccId",method = RequestMethod.GET)
    public Map<String,Object> selectDefaultReceiveAddressByAccId(Integer ordId){
        Map<String,Object> map = new HashMap<>();
        Order order = this.orderService.selectOrderByOrdId(ordId);
        ReceiveAddress defaultReceiveAddress;
        try{
            defaultReceiveAddress = this.accountService.selectReceiveAddressByReAddressId(order.getReAddressId());
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "查询默认收获地址失败");
            return map;
        }
        map.put("code", 1001);
        map.put("msg", "查询默认收获地址成功");
        map.put("defaultReceiveAddress",defaultReceiveAddress);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/selectReceiveAddressByReAddressId",method = RequestMethod.GET)
    public Map<String,Object> selectReceiveAddressByReAddressId(Integer reAddressId){
        Map<String,Object> map = new HashMap<>();
        ReceiveAddress receiveAddress;
        try{
            receiveAddress = this.accountService.selectReceiveAddressByReAddressId(reAddressId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "查询收获地址失败");
            return map;
        }
        map.put("code", 1001);
        map.put("msg", "查询收获地址成功");
        map.put("receiveAddress",receiveAddress);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/selectReceiveAddressByAccId",method = RequestMethod.GET)
    public Map<String,Object> selectReceiveAddressByAccId(Integer accId){
        Map<String,Object> map = new HashMap<>();
        List<ReceiveAddress> receiveAddressList;
        try{
            receiveAddressList = this.accountService.selectReceiveAddressByAccId(accId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "查询所有收获地址失败");
            return map;
        }
        map.put("code", 1001);
        map.put("msg", "查询所有收获地址成功");
        map.put("receiveAddressList",receiveAddressList);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/opinionGoods",method = RequestMethod.POST)
    public Map<String,Object> opinionGoods(Integer opiId, Integer detId, Integer goId, String goNm, Integer accId, String opiType, String opiContent,HttpServletRequest request) {
        Map<String,Object> map = new HashMap<>();
        Integer opiIds;
        try{
            opiIds = this.accountService.opinionGoods(opiId,detId, goId, goNm, accId, opiType, opiContent, request);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "商品评价失败");
            return map;
        }
        map.put("code", 1001);
        map.put("msg", "商品评价成功");
        map.put("opiId",opiIds);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/opinionInfo",method = RequestMethod.GET)
    public Map<String,Object> opinionInfo(Integer detId) {
        Map<String,Object> map = new HashMap<>();
        Opinion opinion = null;
        try{
            opinion = this.accountService.selectMyOpinionByDetId(detId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "商品评价信息查询失败");
            return map;
        }
        map.put("code", 1001);
        map.put("msg", "商品评价信息查询成功");
        map.put("opinion",opinion);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteMyOpinion",method = RequestMethod.DELETE)
    public Map<String,Object> deleteMyOpinion(Integer opiId) {
        Map<String,Object> map = new HashMap<>();
        try{
            this.accountService.deleteMyOpinion(opiId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "评价删除失败");
            return map;
        }
        map.put("code", 1001);
        map.put("msg", "评价删除成功");
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/selectMyOpinion",method = RequestMethod.GET)
    public Map<String,Object> selectMyOpinion(Integer accId,@RequestParam("page") Integer pageCurrent, @RequestParam("limit") Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        List<Map<String,Object>> opinions = null;
        int opinionsNum;
        try{
            opinions = this.accountService.selectMyOpinion(accId, pageCurrent, pageSize);
            opinionsNum = this.accountService.selectMyOpinionNum(accId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "查询评价失败");
            return map;
        }
        map.put("code", 1001);
        map.put("msg", "查询评价成功");
        map.put("opinions",opinions);
        map.put("opinionsNum",opinionsNum);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/selectOpinionFile",method = RequestMethod.GET)
    public Map<String,Object> selectOpinionFile(Integer opiId) {
        Map<String,Object> map = new HashMap<>();
        List<String> imgUrl = null;
        try{
            imgUrl = this.accountService.selectOpinionFile(opiId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "查询评价图片失败");
            return map;
        }
        map.put("code", 1001);
        map.put("msg", "查询评价图片成功");
        map.put("imgUrl",imgUrl);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/updateMyReAddress",method = RequestMethod.PUT)
    public Map<String,Object> updateMyReAddress(ReceiveAddress receiveAddress){
        Map<String,Object> map = new HashMap<>();
        try{
            this.accountService.updateMyReAddress(receiveAddress);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "收获地址修改失败");
            return map;
        }
        map.put("code", 1001);
        map.put("msg", "收获地址修改成功");
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteMyReAddress",method = RequestMethod.DELETE)
    public Map<String,Object> deleteMyReAddress(Integer reAddressId){
        Map<String,Object> map = new HashMap<>();
        try{
            this.accountService.deleteMyReAddress(reAddressId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "收获地址删除失败");
            return map;
        }
        map.put("code", 1001);
        map.put("msg", "收获地址删除成功");
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/updateMyReAddressDefault",method = RequestMethod.PUT)
    public Map<String,Object> updateMyReAddressDefault(Integer accId, Integer reAddressId){
        Map<String,Object> map = new HashMap<>();
        List<ReceiveAddress> receiveAddressList = this.accountService.selectReceiveAddressByAccId(accId);
        try{
            this.accountService.updateMyReAddressDefault(accId, reAddressId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "修改默认收获地址失败");
            return map;
        }
        map.put("code", 1001);
        map.put("msg", "修改默认收获地址成功");
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/addMyReAddress",method = RequestMethod.POST)
    public Map<String,Object> addMyReAddress(ReceiveAddress receiveAddress){
        Map<String,Object> map = new HashMap<>();
        try{
            this.accountService.addMyReAddress(receiveAddress);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "新增收获地址失败");
            return map;
        }
        map.put("code", 1001);
        map.put("msg", "新增收获地址成功");
        return map;
    }
}
